package com.example.musicapi.service;

import com.example.musicapi.common.util.RecommendItem;
import com.example.musicapi.common.util.RecommendUser;
import com.example.musicapi.common.util.RecommendUtil;
import com.example.musicapi.dao.RecordDao;
import com.example.musicapi.dao.StatDao;
import com.example.musicapi.entity.Record;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class RecordService {
    @Autowired
    private RecordDao recordDao;
    @Autowired
    private StatDao statDao;

    public Record add(Record s){
        return recordDao.save(s);
    }
    public void delete(String id){
        recordDao.deleteById(id);
    }


    public List<Record> search(String key){
        return recordDao.findByUser(key);
    }


    /**
     * 推荐列表
     * @param currentUsersId  用户id
     * @return  推荐列表
     */
    public List<String> recommendList(String currentUsersId) {
        /* 创建一个工具类对象 */
        RecommendUtil recommendUtil = new RecommendUtil();
        Map<String,Object> paramsMap = new HashMap<String, Object>();
        /* 存储所有的推荐列表 */
        List<Map<String,Object>> recomeList = this.statDao.recomeList(paramsMap);
        /* 准备数据 */
        /* 当前用户 */
        RecommendUser currentRecommendUser = new RecommendUser();
        currentRecommendUser.setId(currentUsersId);
        List<RecommendItem> currGoodsList = currentRecommendUser.getRecommendItemList();
        /* 其它用户 */
        List<RecommendUser> otherRecommendUserList = new ArrayList<>();
        /* 键是用户Id,值是List对象 */
        Map<String,RecommendUser> otherMap = new HashMap<>();

        for (Iterator iterator = recomeList.iterator(); iterator.hasNext();) {
            Map<String, Object> dataMap = (Map<String, Object>) iterator.next();
            String usersId = dataMap.getOrDefault("usersId", "").toString();
            String goodsId = dataMap.getOrDefault("goodsId", "").toString();
            String score = dataMap.getOrDefault("score", "0").toString();
            RecommendItem recommendGood = new RecommendItem();
            recommendGood.setId(goodsId);
            recommendGood.setScore(Integer.parseInt(score));

            if (currentUsersId.equalsIgnoreCase(usersId)) {
                /* 当前用户 */
                currGoodsList.add(recommendGood);
            } else {
                RecommendUser userDataTemp = otherMap.get(usersId);
                if (userDataTemp == null) {
                    /* 不存在,直接放进去 */
                    userDataTemp = new RecommendUser();
                    userDataTemp.setId(usersId);
                    otherMap.put(usersId, userDataTemp);
                }

                List<RecommendItem> recommendGoodList = userDataTemp.getRecommendItemList();
                recommendGoodList.add(recommendGood);
            }
        }
        for (Iterator iterator = otherMap.entrySet().iterator(); iterator.hasNext();) {
            Map.Entry me = (Map.Entry) iterator.next();
            RecommendUser userTemp = (RecommendUser) me.getValue();
            otherRecommendUserList.add(userTemp);
        }

        List<String> goodsList = new ArrayList<String>();
        /* 计算过同过滤推荐 */
        List<RecommendItem> resultList = recommendUtil.recommend(otherRecommendUserList, currentRecommendUser);
        for (Iterator iterator = resultList.iterator(); iterator.hasNext();) {
            RecommendItem recommendGood = (RecommendItem) iterator.next();
            String goodsId = recommendGood.getId();
            /* 歌曲id */
            goodsList.add(goodsId);
        }
        log.info("==推荐的歌曲信息:{}===", goodsList);
        if (goodsList.isEmpty()) {
            /* 如果推荐为空,则随机生成几首歌曲 */
        }
        return goodsList;
    }
}

